﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using GalaSoft.MvvmLight.Messaging;
using SkyShell.Model;
using GalaSoft.MvvmLight.Threading;

namespace SkyShell
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        double _RecentWindowHeight;

        public MainWindow()
        {
            InitializeComponent();
            _Timer.Tick += timer_Tick;
            Messenger.Default.Register<NotificationMessage<NavigationTargets>>(this, msg =>
                {
                    switch (msg.Content)
                    {
                        case NavigationTargets.SyncModeSelection:
                            {
                                frame1.Navigate(new Uri("/SyncModeSelection.xaml", UriKind.Relative));
                                break;
                            }
                        case NavigationTargets.SideBySideNaming:
                            {
                                frame1.Navigate(new Uri("/SideBySideNaming.xaml", UriKind.Relative));
                                
                                break;
                            }
                        case NavigationTargets.StopSync:
                            {
                                frame1.Navigate(new Uri("/StopSync.xaml", UriKind.Relative));

                                break;
                            }
                        case NavigationTargets.Share:
                            {
                                this.Height = 500;
                                frame1.Navigate(new Uri("/ShareContent.xaml", UriKind.Relative));
                                
                                break;
                            }
                        case NavigationTargets.Authenticate:
                            {
                                DispatcherHelper.CheckBeginInvokeOnUI(() =>
                                    {
                                       // this.Top = this.Top - 300;
                                        cancelButton.Visibility = System.Windows.Visibility.Collapsed;
                                        this.Height = 620;
                                        //this.Width = 500;
                                        frame1.Navigate(new Uri("/SkyShellAuthPage.xaml", UriKind.Relative));
                                   
                                       // resize(700, this.ActualWidth);
                                    });
                                break;
                            }

                        case NavigationTargets.Back:
                            {
                                if (frame1.CanGoBack)
                                    frame1.GoBack();
                                break;
                            }
                        default:
                            break;
                    }
                });
            this.Loaded += new RoutedEventHandler(MainWindow_Loaded);
        }

        string _option;
        void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            string[] args = Environment.GetCommandLineArgs().Skip(1).ToArray();
            if (args.Length > 0)
            {
                _option = args[0];
                switch (_option)
                {
                    case "/sync":
                        {
                            frame1.Navigate(new Uri("/SyncModeSelection.xaml", UriKind.Relative));
                            break;
                        }
                    case "/localsync":
                        {
                            frame1.Navigate(new Uri("/SideBySideNaming.xaml", UriKind.Relative));
                            break;
                        }
                    case "/stop":
                        {
                            frame1.Navigate(new Uri("/StopSync.xaml", UriKind.Relative));
                            break;
                        }
                    case "/sharecontent":
                        {
                            cancelButton.Visibility = System.Windows.Visibility.Collapsed;
                            double newWidth = 1024;
                          //  double newHeight = 450;
                            Left = Left - ((newWidth - this.ActualWidth) / 2);
                            Width = newWidth;
                          //  this.Height = 620;
                            frame1.Navigate(new Uri("/SkyShellAuthPage.xaml", UriKind.Relative));
                            break;
                        }
                    default:
                        break;
                }

            }


            //  Messenger.Default.Register<NotificationMessage<double>>(this, (t) => DispatcherHelper.CheckBeginInvokeOnUI(() =>
            //{
            //    if(t.Notification == "SetWindowHeightg"
            //}
            Messenger.Default.Register<Tuple<string,int>>(this, (t) => DispatcherHelper.CheckBeginInvokeOnUI(() =>
            {
                //if (t.Item1 == "Change Window Height")
                //{
                //    _RecentWindowHeight = this.ActualHeight;
                //    this.Height = t.Item2;
                //}
                //else if (t.Item1 == "Restore Window Height")
                //{
                //    this.Height = _RecentWindowHeight;
                //}

            }));
        }


        static System.Windows.Forms.Timer _Timer = new System.Windows.Forms.Timer();
        int _Stop = 0;
        private double _RatioHeight = 0;
        private double _RatioWidth = 0;

        private double _Height = 0;
        private double _Width = 0;

        private void timer_Tick(Object myObject, EventArgs myEventArgs)
        {
            if (_Stop == 0)
            {
                _RatioHeight = ((this.Height - _Height) / 12) * -1;
                _RatioWidth = ((this.Width - _Width) / 12) * -1;
            }
            _Stop++;

            this.Height += _RatioHeight;
            this.Width += _RatioWidth;

            if (_Stop == 12)
            {
                _Timer.Stop();
                _Timer.Enabled = false;
                _Timer.Dispose();

                _Stop = 0;

                this.Height = _Height;
                this.Width = _Width;
            }
        }

        public void resize(double _PassedHeight, double _PassedWidth)
        {
            _Height = _PassedHeight;
            _Width = _PassedWidth;

            _Timer.Enabled = true;
            _Timer.Start();
        }

        private void OnClose(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

     

    }
}
